<template>
  <div class="car-match-add">
    <div class="car-match-form">
      <el-row>
        <el-form ref="form" :rules="rules" :model="form" label-width="100px">
          <el-col :span="8" :offset="2">
            <el-form-item label="车队名称*" prop="title">
              <el-input v-model="form.title"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="2">
            <el-form-item label="队长姓名" prop="captainname">
              <el-input v-model="form.captainname"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="18" :offset="2">
            <el-form-item label="车队介绍" prop="content">
              <el-input type="textarea" v-model="form.content"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="2">
            <el-form-item label="email*" prop="mail">
              <el-input v-model="form.mail"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="2">
            <el-form-item label="手机号码*" prop="mobile">
              <el-input v-model="form.mobile"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="18" :offset="2">
            <el-form-item label="通讯地址" prop="address">
              <el-input v-model="form.address"></el-input>
            </el-form-item>
            <el-form-item label="赞助商" prop="sponsor">
              <el-input v-model="form.sponsor"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="18" :offset="2">
            <el-form-item label="LOGO">
              <upload
                ref="logo"
                class="upload-demo"
                action="http://game.zhuotone.cn/api/common/upload?lang=zh-cn"
                :data="uploadData"
                :on-remove="handleRemove"
                :on-success="handlSuccess"
                :on-error="handlError"
                :limit="1"
                accept="image/*"
                list-type="picture">
                <el-button size="small" type="primary">上传图片</el-button>
                <span class="upload-tips">(建议尺寸100px*100px)</span>
              </upload>
            </el-form-item>
            <el-form-item label="微信订阅号:">
              <upload
                ref="wx"
                class="upload-demo"
                action="http://game.zhuotone.cn/api/common/upload?lang=zh-cn"
                :limit="1"
                accept="image/*"
                :data="uploadData"
                :on-remove="handleOtherRemove"
                :on-error="handlError"
                :on-success="handlOtherSuccess"
                list-type="picture">
                <el-button size="small" type="primary">上传图片</el-button>
                <span class="upload-tips">(建议尺寸100px*100px)</span>
              </upload>
            </el-form-item>
            <el-form-item label="车迷俱乐部:">
              <upload
                ref="car"
                class="upload-demo"
                action="http://game.zhuotone.cn/api/common/upload?lang=zh-cn"
                :limit="1"
                accept="image/*"
                :data="uploadData"
                :on-remove="handleCarRemove"
                :on-error="handlError"
                :on-success="handlCarSuccess"
                list-type="picture">
                <el-button size="small" type="primary">上传图片</el-button>
                <span class="upload-tips">(建议尺寸100px*100px)</span>
              </upload>
            </el-form-item>
          </el-col>
          
          <el-col :span="24" class="book">
            <div>
                <el-button class="subt" @click="goAdd">创建</el-button>
                <el-button class="subt" @click="resetForm('form')">取消</el-button>
            </div>
          </el-col>
        </el-form>
      </el-row>
    </div>
  </div>
</template>
<script>
import { Upload } from 'element-ui'
import { mapGetters } from 'vuex'
import { carAdd } from '@/api'
export default {
  components: {
    Upload
  },
  data() {
    return {
      rules: {
        title: [
          {
            required: true,
            message: "请输入车队名称",
            trigger: "blur",
          },
        ],
        mail: [
          {
            required: true,
            message: "请输入e-mail",
            trigger: "blur",
          },
        ],
        mobile: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
          },
        ]
      },
      form: {
        title: '',
        captainname: '',
        content: '',
        mail: '',
        mobile: '',
        address: '',
        sponsor: '',
        logoimage: '',
        wximage: '',
        clubimage: ''
      }
    };
  },
  methods: {
      resetForm(formName) {
        this.$refs.logo.clearFiles()
        this.handleRemove()
        this.$refs.wx.clearFiles()
        this.handleOtherRemove()
        this.$refs.car.clearFiles()
        this.handleCarRemove()

        this.$refs[formName].resetFields();
      },
      handleRemove() {
        this.form.logoimage = ''
      },
      handlSuccess(file) {
        this.form.logoimage = file.data.url
      },
      handleOtherRemove() {
        this.form.wximage = ''
      },
      handlOtherSuccess(file) {
        this.form.wximage = file.data.url
      },
      handleCarRemove() {
        this.form.clubimage = ''
      },
      handlCarSuccess(file) {
        this.form.clubimage = file.data.url
      },
      goAdd () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            carAdd(this.form)
              .then((res) => {
                console.log(res);
                if (res.code === 1) {
                  this.$message({ message: res.msg, type: "success" });
                } else {
                  this.$message(res.data.msg);
                }
              })
              .catch((res) => {
                console.log(res);
                this.$message("请稍后再试");
              });
          }
        });
      },
      handlError (err) {
        let res = JSON.parse(err.message)
        this.$message({
          type: 'error',
          message: res.msg
        })
      }
  },
  computed: {
    ...mapGetters({
      userInfo: 'getUserInfo'
    }),
    typeName () {
      //  1企业 2嘉宾 3团体 4个人
      const type = ['', '企业通道', '嘉宾通道', '团体通道', '个人通道']
      return type[this.$route.params.type]
    },
    uploadData () {
      return this.userInfo ?  {token: this.userInfo.token} : {}
    }
  }
};
</script>
<style scoped>
.car-match-add {
  width: 995px;
      float: right;
    margin-right: 2px;
}
.brand {
  color: #aba9a0;
  font-size: 14px;
  height: 66px;
  line-height: 66px;
  margin-top: 6px;
  padding-left: 48px;
}
.brand .active {
  color: #0143a0;
}
.car-match-form {
  padding-top: 50px;
  padding-bottom: 50px;
  border-radius: 10px;
  background-color: rgb(255, 255, 255);
  box-shadow: 0px 5px 10px 0px rgba(0, 0, 0, 0.2);
  width: 995px;
}
.book {
  text-align: center;
  font-size: 14px;
  margin-top: 10px;
}
.book span {
    vertical-align: middle;
}
.subt {
  background-color: #0143a0;
  color: #fff;
  font-size: 24px;
  margin-top: 34px;
}
.subt:first-child {
  margin-right: 20px;
}
.upload-tips {
  font-size: 12px;
  color: #999;
  display: inline-block;
  margin-left: 10px;
}
</style>
<style>
  .el-form-item.is-required:not(.is-no-asterisk) .el-form-item__label-wrap>.el-form-item__label:before, .el-form-item.is-required:not(.is-no-asterisk)>.el-form-item__label:before {
    content: '';
}
</style>